﻿using System;
using System.Text;
using System.Linq;
using System.Web.Mvc;
using Mvcmediaplayer.ViewModels;
using Mvcmediaplayer.Models;

namespace Mvcmediaplayer.Helpers
{
    public static class HTMLHelpers
    {
        public static string GravatarImage(this HtmlHelper helper,string email,string title)
        {
            return String.Format("<img src='http://www.gravatar.com/avatar/{0}.jpg?s=48' alt='{1}' />", "nic162534@gmail.com", title);
        }
       
        public static string Ratings(this HtmlHelper helper, int Id) 
        {
            float average = 0;
             mediaEntities mediaDB = new mediaEntities();
             var video = mediaDB.Videos.Single(v => v.Id == Id);

            if (video.Ratings.Any())
                average = video.Ratings.Average(x => x.Rating1);

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<span class='rating' rating='{0}' video='{1}' title='{2}'>", average, video.Id, "Click to rating");
            string formatStr = "<img src='../../images/{0}' alt='star' width='5' height='12' class='star' value='{1}' />";

            for (double i = .5; i <= 5.0; i = i + .5)
            {
                if (i <= (double)average)
                {
                    sb.AppendFormat(formatStr, (i * 2) % 2 == 1 ? "star-left-on.gif" : "star-right-on.gif", i);
                }
                else
                {
                    sb.AppendFormat(formatStr, (i * 2) % 2 == 1 ? "star-left-off.gif" : "star-right-off.gif", i);
                }
            }

            if (video.Ratings.Any())
                sb.AppendFormat("&nbsp;<span>Currently rated {0:f2} by {1} people</span>", average, video.Ratings.Count);
            else
                sb.AppendFormat("&nbsp;<span>{0}</span>", "Be the first to rate this post");
            sb.Append("</span>");
            return sb.ToString();
        }
    }
}